// Mock数据，模拟后端返回的数据结构

// 模拟用户数据
const mockUsers = [
  {
    id: 1,
    username: '小明',
    avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
    bio: '热爱生活，喜欢分享'
  },
  {
    id: 2,
    username: '小红',
    avatar: 'https://randomuser.me/api/portraits/women/44.jpg',
    bio: '记录每一天的美好'
  },
  {
    id: 3,
    username: '小李',
    avatar: 'https://randomuser.me/api/portraits/men/22.jpg',
    bio: '摄影爱好者'
  }
]

// 模拟打卡数据
const mockCheckIns = [
  {
    id: 1,
    userId: 1,
    content: '今天去爬山了，风景真不错！⛰️\n呼吸新鲜空气，心情舒畅。',
    imageUrls: [
      'https://picsum.photos/800/600?random=1',
      'https://picsum.photos/800/600?random=2'
    ],
    location: '香山公园',
    latitude: 39.9985,
    longitude: 116.1867,
    createTime: '2024-10-16 10:30:00',
    likeCount: 28,
    commentCount: 5,
    isLiked: false,
    user: mockUsers[0]
  },
  {
    id: 2,
    userId: 2,
    content: '分享今天做的美食！🍰\n自己动手做的蛋糕，味道还不错～',
    imageUrls: [
      'https://picsum.photos/800/600?random=3',
      'https://picsum.photos/800/600?random=4',
      'https://picsum.photos/800/600?random=5'
    ],
    location: '家里',
    latitude: 39.9087,
    longitude: 116.3974,
    createTime: '2024-10-16 09:15:00',
    likeCount: 45,
    commentCount: 12,
    isLiked: true,
    user: mockUsers[1]
  },
  {
    id: 3,
    userId: 3,
    content: '今天完成了5公里跑步目标！💪\n坚持运动，保持健康！',
    imageUrls: [
      'https://picsum.photos/800/600?random=6'
    ],
    location: '朝阳公园',
    latitude: 39.9289,
    longitude: 116.4814,
    createTime: '2024-10-15 18:45:00',
    likeCount: 36,
    commentCount: 8,
    isLiked: false,
    user: mockUsers[2]
  },
  {
    id: 4,
    userId: 1,
    content: '阅读时间 📚\n今天读完了这本书，收获满满！',
    imageUrls: [
      'https://picsum.photos/800/600?random=7',
      'https://picsum.photos/800/600?random=8'
    ],
    location: '图书馆',
    latitude: 39.9225,
    longitude: 116.3972,
    createTime: '2024-10-15 15:30:00',
    likeCount: 22,
    commentCount: 3,
    isLiked: false,
    user: mockUsers[0]
  },
  {
    id: 5,
    userId: 2,
    content: '新入手的相机 📷\n迫不及待想出去拍照了！',
    imageUrls: [
      'https://picsum.photos/800/600?random=9',
      'https://picsum.photos/800/600?random=10'
    ],
    location: '数码城',
    latitude: 39.9075,
    longitude: 116.4135,
    createTime: '2024-10-14 14:20:00',
    likeCount: 58,
    commentCount: 15,
    isLiked: true,
    user: mockUsers[1]
  }
]

// 模拟评论数据
const mockComments = [
  {
    id: 1,
    checkInId: 1,
    userId: 2,
    content: '风景真美，下次一起去！',
    createTime: '2024-10-16 11:05:00',
    user: mockUsers[1]
  },
  {
    id: 2,
    checkInId: 1,
    userId: 3,
    content: '这个角度拍得真好！',
    createTime: '2024-10-16 11:20:00',
    user: mockUsers[2]
  },
  {
    id: 3,
    checkInId: 2,
    userId: 1,
    content: '看起来好好吃！能分享一下做法吗？',
    createTime: '2024-10-16 09:40:00',
    user: mockUsers[0]
  },
  {
    id: 4,
    checkInId: 2,
    userId: 3,
    content: '卖相真不错！',
    createTime: '2024-10-16 09:55:00',
    user: mockUsers[2]
  }
]

// 获取当前用户（模拟）
const currentUser = mockUsers[0]

// 格式化时间的工具函数
const formatTime = (timeStr) => {
  const date = new Date(timeStr)
  const now = new Date()
  const diff = now - date
  const minutes = Math.floor(diff / 60000)
  const hours = Math.floor(diff / 3600000)
  const days = Math.floor(diff / 86400000)
  
  if (minutes < 1) return '刚刚'
  if (minutes < 60) return `${minutes}分钟前`
  if (hours < 24) return `${hours}小时前`
  if (days < 7) return `${days}天前`
  
  return date.toLocaleDateString('zh-CN')
}

// 为mock数据添加格式化后的时间
mockCheckIns.forEach(checkIn => {
  checkIn.formattedTime = formatTime(checkIn.createTime)
})

mockComments.forEach(comment => {
  comment.formattedTime = formatTime(comment.createTime)
})

// 导出mock数据和相关函数
export const mock = {
  checkIns: mockCheckIns,
  comments: mockComments,
  users: mockUsers,
  currentUser,
  
  // 模拟API接口的函数
  getCheckInList: (page = 1, pageSize = 10) => {
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = mockCheckIns.slice(start, end)
    return {
      list,
      total: mockCheckIns.length,
      page,
      pageSize
    }
  },
  
  getCheckInDetail: (id) => {
    const checkIn = mockCheckIns.find(item => item.id === Number(id))
    if (checkIn) {
      const comments = mockComments.filter(comment => comment.checkInId === Number(id))
      return {
        ...checkIn,
        comments
      }
    }
    return null
  },
  
  getUserCheckIns: (userId, page = 1, pageSize = 10) => {
    const userCheckIns = mockCheckIns.filter(item => item.userId === Number(userId))
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = userCheckIns.slice(start, end)
    return {
      list,
      total: userCheckIns.length,
      page,
      pageSize
    }
  },
  
  toggleLike: (id) => {
    const checkIn = mockCheckIns.find(item => item.id === Number(id))
    if (checkIn) {
      checkIn.isLiked = !checkIn.isLiked
      checkIn.likeCount += checkIn.isLiked ? 1 : -1
      return {
        isLiked: checkIn.isLiked,
        likeCount: checkIn.likeCount
      }
    }
    return null
  },
  
  createComment: (checkInId, content) => {
    const newComment = {
      id: mockComments.length + 1,
      checkInId: Number(checkInId),
      userId: currentUser.id,
      content,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
      user: currentUser
    }
    newComment.formattedTime = formatTime(newComment.createTime)
    mockComments.push(newComment)
    
    // 更新打卡的评论数
    const checkIn = mockCheckIns.find(item => item.id === Number(checkInId))
    if (checkIn) {
      checkIn.commentCount += 1
    }
    
    return newComment
  },
  
  createCheckIn: (data) => {
    const newCheckIn = {
      id: mockCheckIns.length + 1,
      userId: currentUser.id,
      content: data.content,
      imageUrls: data.imageUrls || [],
      location: data.location || '',
      latitude: data.latitude || 0,
      longitude: data.longitude || 0,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
      likeCount: 0,
      commentCount: 0,
      isLiked: false,
      user: currentUser
    }
    newCheckIn.formattedTime = formatTime(newCheckIn.createTime)
    mockCheckIns.unshift(newCheckIn)
    return newCheckIn
  }
}

export default mock